package com.africanmoviesmobile.ui;

import java.io.IOException;

import org.pixmob.httpclient.HttpClientException;

import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.africanmoviesmobile.R;
import com.africanmoviesmobile.webservice.AmmWebService;
import com.africanmoviesmobile.webservice.BasicAsyncCallback;
import com.africanmoviesmobile.webservice.WebServiceException;

public class TokenActivity extends BaseActivity implements OnClickListener {

	private ProgressDialog mProgressDialog;
	
	private Button mBuyToken;
	private Button mRedeemToken;
	private TextView mTokenBalance;
	private EditText mTokenNumber;
	
	AmmWebService mAmmWebService;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.token_activity);

		mBuyToken = (Button) findViewById(R.id.buy_token);
		mBuyToken.setOnClickListener(this);
		
		mRedeemToken = (Button) findViewById(R.id.redeem_token);
		mRedeemToken.setOnClickListener(this);
		
		mTokenNumber = (EditText) findViewById(R.id.token);
		
		mTokenBalance = (TextView) findViewById(R.id.token_balance);
		
		mProgressDialog = new ProgressDialog(this);
		mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		mProgressDialog.setIndeterminate(true);
		mProgressDialog.setCancelable(false);
		mProgressDialog.setMessage("Please wait...");

		mAmmWebService = AmmWebService.getInstance(this);
		
		setTokenBalance();
		
	}

	@Override
	public void onClick(View view) {

		switch (view.getId()) {
			case R.id.buy_token:
				Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(BUT_TOKEN_URL));
				startActivity(browserIntent);
				break;
			case R.id.redeem_token:
				redeemToken();
				break;	
		}
		
	}
	
	private void setTokenBalance(){
		
		mAmmWebService.getUserTokenBalance(getLoginEmail(), new BasicAsyncCallback() {

			public void onComplete(final int[] result, final Object state) {
				final int tokenBalance = (int) result[0];
				runOnUiThread(new Runnable() {public void run() { mTokenBalance.setText(Integer.toString(tokenBalance)); }});
			}

			public void onHttpClientException(final HttpClientException e, final Object state) {
				Log.w("AMM HttpClientException:", e.getMessage());
			}
			
			public void onIOException(final IOException e, final Object state) {
				Log.w("AMM IOException:", e.getMessage());
			}

			public void onWebServiceException(final WebServiceException e, final Object state) {
				Log.w("AMM WebServiceException:", e.getMessage());
				toastMessage(e.getMessage());
			}

		}, null);
	}

	private void redeemToken() {

		if (mTokenNumber.length() == 0) {
			
			alertMessage(R.string.enter_token_number);
			
		} else {
		
			mProgressDialog.show();
			
			mAmmWebService.redeemToken(getLoginEmail(), mTokenNumber.getText().toString(), new BasicAsyncCallback() {

				public void onComplete(final boolean[] result, final Object state) {

					boolean response = (boolean) result[0];
					
					runOnUiThread(new Runnable() {public void run() { mProgressDialog.dismiss(); }});
					
					if(response){
						toastMessage(R.string.token_redeemed);
						runOnUiThread(new Runnable() {public void run() { mTokenBalance.setText(R.string.loading); }});
						setTokenBalance();
					}else{
						toastMessage(R.string.token_redeem_error);
					}

					runOnUiThread(new Runnable() {public void run() { mTokenNumber.setText(""); }});
				}

				public void onHttpClientException(final HttpClientException e, final Object state) {
					runOnUiThread(new Runnable() {public void run() { mProgressDialog.dismiss(); }});
					Log.w("AMM HttpClientException:", e.getMessage());
				}
				
				public void onIOException(final IOException e, final Object state) {
					runOnUiThread(new Runnable() {public void run() { mProgressDialog.dismiss(); }});
					Log.w("AMM IOException:", e.getMessage());
				}

				public void onWebServiceException(final WebServiceException e, final Object state) {
					runOnUiThread(new Runnable() {public void run() { mProgressDialog.dismiss(); }});
					Log.w("AMM WebServiceException:", e.getMessage());
					toastMessage(e.getMessage());
				}

			}, null);
			
		}
		
	}

}
